//
//  TheModule_5_Table.m
//  CaiPiao
//
//  Created by l wb on 2018/5/16.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "TheModule_5_Table.h"
#import "PhotoController.h"
@interface TheModule_5_Table ()
@property (weak, nonatomic) IBOutlet UIButton *headImg;

@property (weak, nonatomic) IBOutlet UIButton *displayNameBtn;


@end

@implementation TheModule_5_Table

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的";



    
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getPersonalDataFromNet];
}

- (IBAction)nameBtnClickAction:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"登陆/注册"]) {
        [OpenLoginManager openLoginPageWithController:self];
    }
    
}

-(void)getPersonalDataFromNet{
    //查找_User表
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"_User"];
    [bquery getObjectInBackgroundWithId:kUserID block:^(BmobObject *object, NSError *error) {
 
        if (error) {
            if (kUserID.length == 0) {
                
            } else {
                [EasyTextView showErrorText:@"获取数据失败..."];
            }
            
        } else {
            if ([object objectForKey:@"headImg"]) {
                [self.headImg sd_setImageWithURL:[NSURL URLWithString:[object objectForKey:@"headImg"] ] forState:UIControlStateNormal];
               
            }

            if ([object objectForKey:@"displayName"]) {
                
                [self.displayNameBtn setTitle:[object objectForKey:@"displayName"] forState:UIControlStateNormal];
           
            }else{
                if (kUserID) {
                    [self.displayNameBtn setTitle:[object objectForKey:@"username"] forState:UIControlStateNormal];
                }
                
                
            }
            
        }
        
        
    }];
    
}
- (IBAction)addPhotoImgClickAction:(UIButton *)sender {
    if ([OpenLoginManager openLoginPageWithController:self]) {
    return;
    }
    
    
    PhotoController *photo = [PhotoController sharedPhotoWithController:self];
    
    photo.blockImageDone = ^(UIImage *image) {
        [EasyLodingView showLodingText:@"正在努力加载中..." config:^EasyLodingConfig *{
            return [EasyLodingConfig shared].setLodingType(LodingShowTypeIndicator);
        }];
        
        NSData *data = UIImagePNGRepresentation(image);
        BmobFile *file = [[BmobFile alloc] initWithFileName:@"headImg.png" withFileData:data];
        BmobUser *buser = [BmobUser currentUser];
        [file saveInBackground:^(BOOL isSuccessful, NSError *error) {
            //如果文件保存成功，则把文件添加到filetype列
            if (isSuccessful) {
                //上传文件的URL地址
                [buser setObject:file.url  forKey:@"headImg"];
                //此处相当于新建一条记录,         //关联至已有的记录请使用 [obj updateInBackground];
                [buser updateInBackground];
                [EasyLodingView hidenLoding];
                [EasyTextView showSuccessText:@"成功"];
                [self.headImg setImage:image forState:UIControlStateNormal];
                
            }else{
                //进行处理
                [EasyLodingView hidenLoding];
                [EasyTextView showErrorText:error.description];
            }
        }];
        
        
        
        
    };
}

- (IBAction)shareAppClickAction:(UITapGestureRecognizer *)sender {
    NSString *appid = @"id1387753970";
    NSString *url = [NSString stringWithFormat:@"https://itunes.apple.com/cn/app/id%@?mt=8",appid];

    
    NSDictionary *par = @{@"shareUrl":url};
    [WBNativityShare WBSystemCallShareWithController:self par:par];
    
}
- (IBAction)logOutClickAction:(UIButton *)sender {
    [UIAlertController showConfirmWithColorStyle:@{@"headerColor":kNavigationBarColor,@"actionTitleColor":kNavigationBarColor} Title:@"" Message:@"退出登录" andDoneAction:^{
        [BmobUser logout];
        [EasyTextView showSuccessText:@"退出成功"];
        self.tabBarController.selectedIndex = 0;
        [self.navigationController popViewControllerAnimated:YES];
    } andCancelAction:^{
        
    }];
   
}

- (IBAction)goNumberRecordPageClickAction:(UITapGestureRecognizer *)sender {

    [self.navigationController pushViewController:[[UIStoryboard storyboardWithName:@"RecordStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"RecordController"] animated:YES];
}
- (IBAction)goMyArticlePageClickAction:(UITapGestureRecognizer *)sender {

    [self.navigationController pushViewController:[[UIStoryboard storyboardWithName:@"BBSStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"MyArticleController"] animated:YES];
    
}
- (IBAction)contactCustomerService:(UITapGestureRecognizer *)sender {
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"0755-32929768"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    
    

    

    
    
}

@end
